// pages/SCM/takePhoto/takePhoto.js
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArray: [],
    imgArrayRear: [],
    imgArrayLeft: [],
    imgArrayRight: [],
    imgArrayBottom: [],
    maxImgLength: 9, //上传图片张数
    hospitalId: null,
    canShow: false,
    timer: '',
    desc: '',
    submitShow: false,
    tabNum: 1,
    shotTime: false,
    timers: '',
    lbmName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.hospitalId) {
      that.setData({
        hospitalId: options.hospitalId,
        pileCode: options.pileCode ? options.pileCode : '', // 桩号
        equId: options.equId ? options.equId : '', // 轮椅号
        shotTime: true // 展示医院信息
      })
    }
    if (options.data) {
      let data = JSON.parse(options.data)
      that.setData({
        imgArray: data.positivePictureArr ? data.positivePictureArr : [], // 正面图片
        imgArrayRear: data.backPictureArr ? data.backPictureArr : [], // 背面图片
        imgArrayLeft: data.leftPictureArr ? data.leftPictureArr : [], // 左面图片
        imgArrayRight: data.rightPictureArr ? data.rightPictureArr : [], // 右面图片
        imgArrayBottom: data.bottomPictureArr ? data.bottomPictureArr : [], // 下面图片
        desc: data.desc ? data.desc : '', // 备注内容
        hospitalId: data.lbmId ? data.lbmId : '', // 医院id
        id: data.id ? data.id : '', // 主键Id
        positiveArrMap: data.positiveArrMap ? data.positiveArrMap : '', // MAP
        pileCode: data.pileId ? data.pileId : '', // 桩号
        equId: data.equId ? data.equId : '', // 轮椅号
        submitShow: true,
        shotTime: true
      })
      let arrayImage = [];
      arrayImage = arrayImage.concat(that.data.imgArray).concat(that.data.imgArrayRear).concat(that.data.imgArrayLeft).concat(that.data.imgArrayRight).concat(that.data.imgArrayBottom)
      app.globalData.arrayImage = arrayImage
      // console.log(app.globalData.arrayImage,'app.globalData.arrayImage')
    }
    that.getHospitalInfo()
  },
  bindTextAreaBlur(e) {
    console.log(e)
    this.setData({
      desc: e.detail.value
    })
  },
  takePhoto(e) {
    console.log(e)
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let name = e.currentTarget.dataset.id
        that.setData({
          [name]: tempFilePaths
        })
        console.log(that.data.before_0[0])
      }
    })
  },
  // 复制功能
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getHospitalInfo(){
    let that = this
    if (!that.data.isScanning && that.data.hospitalId) {
      lbbService.getHospitalDetail({
        hospitalId: parseInt(that.data.hospitalId)
      }, function (res) {
        console.log(res.basicInfo, '.basicInfo.patroling')
        that.setData({
          hospitalInfo: res,
          pileChairListNew: res.pileChairList,
          pileStretchListNew: res.pileStretchList,
          chairListNew: res.chairList,
          carListNew: res.stretcherList,
          phcListNew: res.pileChaperonList,
          canShow: true
        })
      })
    } else {
      that.setData({
        canShow: true
      })
    }
  },
  setImgArr: function (e) {
    let that = this;
    let imgArray = e.detail;
    that.setData({
      imgArray: imgArray
    });
    console.log(imgArray)
  },
  setImgArrOne(e) {
    let that = this;
    let imgArrayRear = e.detail;
    that.setData({
      imgArrayRear: imgArrayRear
    });
    console.log(imgArrayRear)
  },
  setImgArrTwo(e) {
    let that = this;
    let imgArrayLeft = e.detail;
    that.setData({
      imgArrayLeft: imgArrayLeft
    });
    console.log(imgArrayLeft)
  },
  setImgArrThree(e) {
    let that = this;
    let imgArrayRight = e.detail;
    that.setData({
      imgArrayRight: imgArrayRight
    });
    console.log(imgArrayRight)
  },
  setImgArrFour(e) {
    let that = this;
    let imgArrayBottom = e.detail;
    that.setData({
      imgArrayBottom: imgArrayBottom
    });
    console.log(imgArrayBottom)
  },
  newProblem: function (e) {
    let that = this;
    console.log(that.data.imgArray, '修改拍照上传修改')
    let imgArray = that.data.imgArray;
    if (imgArray.length > 0) {
      console.log(imgArray, '拍照上传修改')
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      util.upLoadImg(app.globalData.header, imgArray, 0, [], that.newProblemBackFir, that.data.positiveArrMap);
    } else {
      console.log('执行了修改拍照上传修改')
      that.newProblemBackFir([]);
    }
    let imgArrayRear = that.data.imgArrayRear;
    if (imgArrayRear.length > 0) {
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      util.upLoadImg(app.globalData.header, imgArrayRear, 0, [], that.newProblemBackOne, that.data.positiveArrMap);
    } else {
      that.newProblemBackOne([]);
    }
    let imgArrayLeft = that.data.imgArrayLeft;
    if (imgArrayLeft.length > 0) {
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      util.upLoadImg(app.globalData.header, imgArrayLeft, 0, [], that.newProblemBackTwo, that.data.positiveArrMap);
    } else {
      that.newProblemBackTwo([]);
    }

    let imgArrayRight = that.data.imgArrayRight;
    if (imgArrayRight.length > 0) {
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      util.upLoadImg(app.globalData.header, imgArrayRight, 0, [], that.newProblemBackThree, that.data.positiveArrMap);
    } else {
      that.newProblemBackThree([]);
    }
    let imgArrayBottom = that.data.imgArrayBottom;
    if (imgArrayBottom.length > 0) {
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      util.upLoadImg(app.globalData.header, imgArrayBottom, 0, [], that.newProblemBackFour, that.data.positiveArrMap);
    } else {
      that.newProblemBackFour([]);
    }
  },
  newProblemBack: function () {
    var that = this
    console.log('成功了', that.data.desc, that.data.pileCode)
    if (that.data.submitShow) {
      lbbService.updateInspectUploadPic({
        id: that.data.id ? that.data.id : 0,
        positivePicture: that.data.imgArray ? that.data.imgArray : [],
        backPicture: that.data.imgArrayRear ? that.data.imgArrayRear : [],
        leftPicture: that.data.imgArrayLeft ? that.data.imgArrayLeft : [],
        rightPicture: that.data.imgArrayRight ? that.data.imgArrayRight : [],
        bottomPicture: that.data.imgArrayBottom ? that.data.imgArrayBottom : [],
        desc: that.data.desc ? that.data.desc : '',
      }, function (res) {
        wx.showToast({
          title: '修改成功',
        })
        console.log('修改成功')
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      })
    } else {
      lbbService.saveInspectUploadPic({
        lbmId: that.data.hospitalId ? that.data.hospitalId : 0,
        positivePicture: that.data.imgArray ? that.data.imgArray : [],
        backPicture: that.data.imgArrayRear ? that.data.imgArrayRear : [],
        leftPicture: that.data.imgArrayLeft ? that.data.imgArrayLeft : [],
        rightPicture: that.data.imgArrayRight ? that.data.imgArrayRight : [],
        bottomPicture: that.data.imgArrayBottom ? that.data.imgArrayBottom : [],
        pileId: that.data.pileCode ? that.data.pileCode : '',
        equId: that.data.equId ? that.data.equId : '',
        equTypeId: that.data.equTypeId ? that.data.equTypeId : '',
        desc: that.data.desc ? that.data.desc : '',
      }, function (res) {
        wx.showToast({
          title: '提交成功',
        })
        console.log('提交成功')
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  newProblemBackFir(res) {
    wx.hideLoading()
    this.setData({
      imgArray: res,
      imgUpTrue: true
    })
    console.log('正面', this.data.imgArray)
  },
  newProblemBackOne(res) {
    wx.hideLoading()
    this.setData({
      imgArrayRear: res,
      imgUpTrueOne: true
    })
    console.log('背面', this.data.imgArrayRear)
  },
  newProblemBackTwo(res) {
    wx.hideLoading()
    this.setData({
      imgArrayLeft: res,
      imgUpTrueTwo: true
    })
    console.log('左面', this.data.imgArrayLeft)
  },
  newProblemBackThree(res) {
    wx.hideLoading()
    this.setData({
      imgArrayRight: res,
      imgUpTrueThree: true
    })
    console.log('右面', this.data.imgArrayRight)
  },
  newProblemBackFour(res) {
    var that = this
    wx.hideLoading()
    that.setData({
      imgArrayBottom: res,
      imgUpTrueFour: true
    })
    that.Ready()
    console.log('下面', that.data.imgArrayBottom)
  },

  Ready() {
    var that = this
    var timer = setInterval(() => {
      if (that.data.imgUpTrueFour && that.data.imgUpTrueThree && that.data.imgUpTrueTwo && that.data.imgUpTrueOne && that.data.imgUpTrue) {
        clearInterval(timer)
        that.newProblemBack()
      }
    }, 1000)
    that.setData({
      timer: timer
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  toggleTab: function (e) {
    console.log(e)
    let tabNum = e.currentTarget.dataset.num
    this.setData({
      tabNum: tabNum
    })
  },
  /**
   * 输入控制盒编号
   */
  inputPileCode: function (e) {
    let that = this;
    let pileCode = e.detail.value;
    that.setData({
      pileCode: pileCode,
    })

    console.log(that.data.pileCode)

  },
  /**
   * 输入轮椅编号
   */
  inputEquipCode: function (e) {
    let that = this;
    let equId = e.detail.value;
    that.setData({
      equId: equId,
    })
    // lbbService.getLbmInfoByEquIdAndEquType({
    //   equId: that.data.equId,
    //   equType:'equ'
    // }, res => {
    //   console.log(res)
    // })
    console.log(that.data.equId)
  },
  PileCodeBlur(e) {
    var that = this
    console.log(e.detail.value)
    lbbService.getLbmInfoByEquIdAndEquType({
      equId: e.detail.value,
      equType: 'pile'
    }, res => {
      console.log(res)
      that.setData({
        hospitalId: res.data && res.data.lbmId ? res.data.lbmId : '',
        pileCode: res.data && res.data.hid ? res.data.hid : '',
        lbmName: res.data && res.data.lbmName ? res.data.lbmName : ''
      })
    })
  },
  EquipCodeBlur(e) {
    var that = this
    lbbService.getLbmInfoByEquIdAndEquType({
      equId: e.detail.value,
      equType: 'equ'
    }, res => {
      console.log(res, '我是轮椅RES')
      that.setData({
        hospitalId: res.data && res.data.lbmId ? res.data.lbmId : '',
        equId: res.data && res.data.equId ? res.data.equId : '',
        lbmName: res.data && res.data.lbmName ? res.data.lbmName : ''
      })
    })
    console.log(e.detail.value, 'VALUE')
  },
  /**
   * 获取控制盒信息列表
   */
  // getPileListInfo: function () {
  //   let that = this;
  //   let reg1 = /[^0-9]+/;
  //   let reg2 = /^[^0-9]+|[^0-9]+$/g;//对非数字去头去尾
  //   let pileCode = that.data.pileCode.replace(reg2,'');
  //   let pileCodeArr = pileCode.split(reg1);
  //   that.setData({
  //     pileCode: pileCode,
  //     pileCodeArr: pileCodeArr
  //   })
  //   if (pileCode) {
  //     lbbService.getPileBatch({
  //       pileCode: pileCode
  //     }, function (res) {
  //       that.setData({
  //         pileInfoList: res.data
  //       })
  //     })
  //   }
  // },
  chooseHospital: function () {
    var that = this
    wx.navigateTo({
      url: '/pages/chooseHospital/chooseHospital',
    })
    var timers = setInterval(() => {
      console.log(that.data.changedLocationInfo, '我是啦啦啦')
      if (that.data.changedLocationInfo) {
        clearInterval(timers)
        that.setData({
          hospitalId: that.data.changedLocationInfo.hospitalId
        })
        console.log(that.data.hospitalId, '我是啦啦啦2')
      } else {
        if (that.data.hospitalId) {
          that.setData({
            hospitalId: that.data.hospitalId
          })
          clearInterval(timers)
        }
      }
    }, 1000)
    that.setData({
      timers: timers
    })
  },
  showLocationModal: function () {
    let that = this
    that.setData({
      isLocationShow: true
    })
  },
  equipTypeChange: function (e) {
    console.log(e)
    this.setData({
      equipType: e.detail.value
    })
  },
  /**
   * 扫码获取控制盒编号
   */
  scanBoxCode: function () {
    let that = this;
    wx.scanCode({
      success(res) {
        console.log('scanCode-->', res);
        let qrc = '';
        let qrCode = res.result;
        if (!qrCode) {
          // that.toast.showToast('二维码为空');
          wx.showToast({
            title: '二维码为空',
          })
          return;
        }
        let reg = /^([A-Za-z0-9]{2}:){5}([A-Za-z0-9]{2}){1}$/;
        if (qrCode.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX) == 0) {
          //控制盒大二维码
          console.log('我是控制盒大码：', qrCode)
          var rst = qrCode.split('/');
          qrc = rst[rst.length - 1];
          that.data.scan_qrc = qrc;
          console.log('我是控制盒大码：', qrc)
        } else if (reg.test(qrCode)) {
          //控制盒小二维码
          that.data.deviceId = qrCode;
          that.data.mac = qrCode.replace(/:/g, '');
          lbbService.getHpIdByMacId({
            mac: that.data.mac
          }, res => {
            that.setData({
              pileCode: res.data.pileId + '',
            })
            lbbService.getLbmInfoByEquIdAndEquType({
              equId: that.data.pileCode,
              equType: 'pile'
            }, res => {
              console.log(res)
            })

            that.getPileInfo();
          })
          return;
        } else {
          // that.toast.showToast('二维码错误');
          wx.showToast({
            title: '二维码有误',
          })
          return
        }
        lbbService.getMacId({
          markCode: that.data.scan_qrc,
          system: 'Android'
        }, function (res) {
          if (res.pileId) {
            that.setData({
              pileCode: res.pileId + '',
            })
            that.getPileInfo();
          } else {
            // that.toast.showToast('获取mac失败');
            wx.showToast({
              title: '获取mac失败',
            })
          }
        })
      },
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})